import Vue from "vue";
import iView from "iview";
import axios from "axios";
import VueAxios from "vue-axios";

import router from "@/router";
import app from "@/app.vue";

import "iview/dist/styles/iview.css";

import config from "@/config";
let apiRoot = "";
if (process.env.NODE_ENV === "development") {
  apiRoot = config.development.apiRoot;
} else if (process.env.NODE_ENV === "production") {
  apiRoot = config.production.apiRoot;
}

axios.defaults.baseURL = apiRoot;
axios.interceptors.request.use(
  config => {
    const authorization = localStorage.getItem("Authorization");
    if (authorization) {
      config.headers.Authorization = "Bearer " + authorization;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);
axios.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    // console.error(error);

    const response = error.response;

    if (response) {
      alert("[" + response.status + "] " + response.data);
    } else if (error.request) {
      alert("服务器没有响应，网络超时。");
    } else {
      alert("Error: " + error.message);
    }

    return Promise.reject(error);
  }
);

Vue.use(VueAxios, axios);
Vue.use(iView);

Vue.config.productionTip = false;

new Vue({
  router,
  render: h => h(app)
}).$mount("#app");
